package database;

import entity.Mark;
import manager.MarkManager;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 *
 * @author Noi
 */
public class MarkManagerDB {
    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;

    public MarkManagerDB(java.sql.Connection conn) {
        this.conn = conn;
    }
    
    public void loadData(MarkManager markManager) {
        try {
            cstmt = conn.prepareCall("{call sp_GetMark}");
            rs =  cstmt.executeQuery();
            while(rs.next()) {
                int subjectID = rs.getInt("SubjectID");
                String studentID = rs.getString("StudentID");
                String firstSemesterValue = rs.getString("FirstSemesterValue");
                String secondSemesterValue = rs.getString("SecondSemesterValue");
                String yearValue = rs.getString("YearValue");
                boolean status = rs.getBoolean("Status");
                Mark mark = new Mark(subjectID, studentID, firstSemesterValue, secondSemesterValue, yearValue, status);
                markManager.add(mark);
            }
            rs.close();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(MarkManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void deleteMark(Mark obj) {
        try {
            cstmt = conn.prepareCall("{call sp_DeleteMark(?,?)}");
            cstmt.setInt(1, obj.getSubjectID());
            cstmt.setString(2, obj.getStudentID());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(MarkManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void insertMark(Mark obj) {
        try {
            cstmt = conn.prepareCall("{call sp_InsertMark(?,?,?,?,?)}");
            cstmt.setInt(1, obj.getSubjectID());
            cstmt.setString(2, obj.getStudentID());
            cstmt.setString(3, obj.getFirstSemesterValue());
            cstmt.setString(4, obj.getSecondSemesterValue());
            cstmt.setString(5, obj.getYearValue());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(MarkManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void updateMark(Mark obj) {
        try {
            cstmt = conn.prepareCall("{call sp_UpdateMark(?,?,?,?,?,?)}");
            cstmt.setInt(1, obj.getSubjectID());
            cstmt.setString(2, obj.getStudentID());
            cstmt.setString(3, obj.getFirstSemesterValue());
            cstmt.setString(4, obj.getSecondSemesterValue());
            cstmt.setString(5, obj.getYearValue());
            cstmt.setBoolean(6, obj.isStatus());
            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(MarkManagerDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

